<?php
use Lib\Encryption\DriverFactory as EncryptionFactory,
    Lib\Encryption as Encryption;

use Lib\Data, 
    Lib\Source as Source,
    Lib\Source\DriverFactory as StorageFactory;
    
class App_Plugin_WebUser extends Zend_Controller_Plugin_Abstract
{
    protected $_id = null;
    protected $_hash = null;
    protected $_secretWord = 'VerySecretWord12';
    
    protected $_host;
    
    private $_sessionNamespace = 'webuser';
    
    /**
     * @var Zend_Session_Namespace
     */
    protected $_session = null;
    
        
    public function preDispatch(Zend_Controller_Request_Abstract $request){
        $webUser = new Data(array(
            'webuser' => $this->getHash()
        ));
//        $encryptor = new Encryption(EncryptionFactory::ENCRYPTION_DRIVER_MCRYPT);
        
        $sessionStorage = new Source(Source::DRIVER_ZEND_SESSION);
        $sessionStorage->load('webuser', false);
        
        $cookieStorage  = new Source(Source::DRIVER_ZEND_COOKIE);
        $cookieStorage->load('webuser', false);
//        $cookieStorage->getStorage()->setEncryptor($encryptor);
        
        if($webuserHash = $sessionStorage->getStorage()->webuser){
            $webUser->webuser = $webuserHash;
        } else {
            if($webuserHash = $cookieStorage->getStorage()->webuser){
                // User is in cookie
                $sessionStorage->setStorage($webUser);
                $sessionStorage->save();
                
                $webUser->webuser = $webuserHash;
            } else {
                // User is first time on site
                
                $sessionStorage->setStorage($webUser);
                $sessionStorage->save();
                
                $cookieStorage->setStorage($webUser);
                $cookieStorage->save();
            }
        }
        
    }
    
    /**
     * 
     * Enter description here ...
     */
    public static function getInfo($hash){
        $webUserManager = new \Managers\WebUser();
        $webUserDb = $webUserManager->getByHash($webUser->webuser);
        $webUserDb = reset($webUserDb);
    }
    /**
     * 
     * Enter description here ...
     */
    public static function loadInfo(){
        
    }
    
    /**
     * 
     * Enter description here ...
     */
    public static function updateInfo(){
        
    }
    
    /**
     * 
     * Enter description here ...
     */
    public static function saveInfo(){
        
    }
    
    /**
     * Retrieves existed or creates new unique hash for webuser
     * @param bool $recreate
     */
    public function getHash($recreate = false){
        if($recreate) {
            $this->_hash = null;
        }
        
        if(null === $this->_hash){
            $this->_hash = md5(microtime(false) . rand(0, 9999) . $this->_secretWord);
        }
        
        return $this->_hash;
    }
    
    /**
     * Retrieves host for setting cookies
     */
    public function getHost(){
        if(null === $this->_host){
            $this->_host = Zend_Controller_Front::getInstance()->getBaseUrl();
        }
        
        return $this->_host;
    }
}
